import { Component, OnInit, Injector, ViewChild } from '@angular/core';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import { Table } from 'primeng/table';
import { Paginator, LazyLoadEvent } from 'primeng/primeng';
import {
    ListDtoExtensionsItem,
    StockType,
    StockZT_RoomServiceProxy,
    StockZT_ToolAccountServiceProxy
} from '@shared/service-proxies/stock-service-proxies';
import { UserDataPermissionServiceProxy } from '@shared/service-proxies/stock-service-proxies';
import { AppConsts } from '@shared/AppConsts';

@Component({
    templateUrl: './tool-account.component.html',
    animations: [appModuleAnimation()]
})
export class ToolAccountComponent extends AppComponentBase implements OnInit {
    @ViewChild('dataTable') dataTable: Table;
    @ViewChild('paginator') paginator: Paginator;

    // filters
    f_MaterialCode = '';
    f_MaterialName = '';
    f_TradeMark = '';
    f_Station = '';
    f_TecCondition = '';
    f_Specification = '';
    f_QualityLevel = '';
    f_InventBatchID = '';
    f_WarehouseName = '';
    f_RealInventory;
    f_Precision = ''; //精度
    userDataPermissions;
    // selectedDepts;
    extensions: Array<ListDtoExtensionsItem>;
    useableOptions = [{ label: '全部', value: undefined }, { label: '可用', value: true }];
    isuseable;
    realInventory;
    scrollHeight = '250px';
    roomOption;
    selectedWareHouse;
    f_SeriousNum;
    selectedAccounts;
    constructor(
        injector: Injector,
        private _accountService: StockZT_ToolAccountServiceProxy,
        private _userDataPermissionServiceProxy: UserDataPermissionServiceProxy,
        private _stockZT_RoomServiceProxy: StockZT_RoomServiceProxy
    ) {
        super(injector);
    }
    ngOnInit(): void {
        this._userDataPermissionServiceProxy.getUserDataPermissions('Warehouses').subscribe(result => {
            this._stockZT_RoomServiceProxy.getRoomListByDepts(result.items, '工具库', '').subscribe(result2 => {
                this.roomOption = result2.items.map(e => {
                    return {
                        label: e.displayText,
                        value: e
                    };
                });
            });
            this._stockZT_RoomServiceProxy.getRoomListByUserRole('工具库').subscribe(result2 => {
                this.selectedWareHouse = result2.items;
                this.getAccounts();
            });

        });

        this.scrollHeight = document.body.clientHeight - 100 + 'px';
        // this._userDataPermissionServiceProxy.getUserDataPermissions('Tool_Accounts').subscribe(result => {
        //     this.userDataPermissions = result.items.map(e => {
        //         return {
        //             label: e.displayText,
        //             value: e
        //         };
        //     });
        //      this._base_SessionServiceProxy.getCurrentDept().subscribe(result => {             let ssData = new ComboboxItemDto();             ssData.value = result.value;             ssData.isSelected = false;             ssData.displayText = result.label;             let ss: ComboboxItemDto[] = [ssData];             this.selectedDepts = ss;         });
        //     this.getAccounts();
        //     // if (this.userDataPermissions.length > 0) {
        //     //     this.selectedDepts = this.userDataPermissions[0].value;
        //     //     this.getAccounts();
        //     // }
        // });
    }

    printBillList() {
        if (this.selectedWareHouse.length != 1) {
            this.notify.error('请选择一个仓库进行打印！');
        } else {
            //装配类派工单
            window.open(
                AppConsts.printAddr + '/webroot/decision/view/report?viewlet=erp/GJTZ.cpt&F_WarehouseName=' +
                    this.selectedWareHouse[0].displayText
            );
        }
    }
    getAccounts(event?: LazyLoadEvent) {
        if (this.primengTableHelper.shouldResetPaging(event) || event.first == undefined) {
            this.paginator.changePage(0);

            return;
        }
        //if(this.selectedDepts.displayText==undefined){return;} getToolAccountListExtension
        this.primengTableHelper.showLoadingIndicator();
        this._accountService
            .getToolAccountListExtension(
                this.f_MaterialCode,
                this.f_MaterialName,
                this.f_RealInventory,
                this.f_SeriousNum,
                this.selectedWareHouse,
                this.f_Specification,
                this.f_TradeMark,
                this.f_Precision,
                // this.selectedDepts.displayText,
                '',
                this.isuseable,
                StockType.工具,
                this.realInventory,
                this.primengTableHelper.getSorting(this.dataTable),
                this.primengTableHelper.getMaxResultCount(this.paginator, event),
                this.primengTableHelper.getSkipCount(this.paginator, event)
            )
            .subscribe(result => {
                this.extensions = result.items;
                this.primengTableHelper.totalRecordsCount = result.dtos.totalCount;
                this.primengTableHelper.records = result.dtos.items;
                this.primengTableHelper.hideLoadingIndicator();
            });
    }
}
